#
#  Weapons.py
#  TwoHand
#
#  Created by Pete Lord on 17/12/2008.
#  Copyright (c) 2008 __MyCompanyName__. All rights reserved.
#
#  Module for wepaons, NOT PROJECTILES.

import pygame, Projectiles # for drawing purposes.

class Weapon(): # player weapons.
    def __init__(self, entities, screen, relativeX, relativeY, parent, previousWeaponPoints):
        self.entities = entities
        self.screen = screen
        self.x = relativeX
        self.y = relativeY
        self.parent = parent
        self.previousPoints = previousWeaponPoints
        self.currentMorph = self.morphTime = 100
        
        self.points = [(-3,0), (-3, -20), (3, -20),(3,0), (0, 0),\
                       (0,0), (0, 0), (0, 0),(0,0), (0, 0),\
                       (0,0), (0, 0), (0, 0),(0,0), (0, 0),\
                       (0,0), (0, 0), (0, 0),(0,0), (0, 0)]
                       
        self.barrel = (0 + self.x + self.parent.x, -20 + self.y + self.parent.y) # where do we shoot from?
        
        self.ammoInClip = self.ammo = 10 # shots. And how many are in each clip.
        self.clips = 5 # clips
        
        self.ready = False
        self.OrderFire = False
        self.OrderReload = False
        
        self.refire = self.refireTime = 10
        
        self.reload = 0
        self.reloadTime = 50
        
    def getXY(self):
        return (self.x, self.y)
        
    def poll(self):
        self.barrel = (0 + self.x + self.parent.x, -20 + self.y + self.parent.y) # where do we shoot from?
        if self.ready: # morphed and ready to fire.
            if self.refire > 0: # refire always decreases...
                self.refire -= 1
            # are we reloading?
            if self.reload > 0:
                if self.clips == 0:
                    # nothing, no clips to reload with!
                    print "No clips." # debug.
                else:
                    if self.reload == 1:
                        self.reload -= 1
                        self.clips -= 1
                        self.ammo = self.ammoInClip
                    else:
                        self.reload -= 1
            else:
                # are we trying to fire.
                if self.OrderFire:
                    if self.ammo == 0:
                        self.reload = self.reloadTime # reloadddd
                    else:
                        if self.refire == 0:
                            self.fire() # pew pew!
                            self.ammo -= 1
                            self.refire = self.refireTime # wait between shots.
        else: # code for morphtastic morphing
            # we interpolate each point from it's last to it's new over the morph time of the weapon.
            # because the morph time is specific to the new weapon, we don't have a problem.
            # weapons always have 20 points. ALWAYS                                                     !!!!!!! AGH!
            self.currentMorph -= 1
            if self.currentMorph == 0:
                self.ready = True
            else: # we morph!
                if self.previousPoints == False: # if we didn't have a previous weapon.
                    for point in self.points:
                        point = (self.interpolate(1, point[0], self.currentMorph), self.interpolate(1, point[1], self.currentMorph))
                else:
                    for i in range(0, 20): # loop through.
                        # this is big and ugly. But ohwell.
                        self.points[i] = (self.interpolate(self.previousPoints[i][0], self.points[i][0], self.currentMorph),\
                                          self.interpolate(self.previousPoints[i][1], self.points[i][1], self.currentMorph))
                                      
    def interpolate(self, a, b, step): #doesn't need steps value, as step goes down to 1 and not below it. Delicious. See morph code.
        x = 0.0
        if a > b:
            x = a - b + (b / step)
        else:
            x = b - a + (a / step)
        return x
                                      
    def offsetPoints(self, list):
        # calculate points for drawing purposes. So that the ship gets drawn at the right location, rather than around 0,0
        # this is the weapon specific version, that draws relative to the parent
        returnList = []
        for point in list:
            returnList.append([(point[0] + self.x + self.parent.x), (point[1] + self.y + self.parent.y)])
        return returnList
                                      
    def draw(self):
        self.points_OFFSET = self.offsetPoints(self.points)
        pygame.draw.polygon(self.screen, (0, 0, 0), self.points_OFFSET, 0)
        pygame.draw.aalines(self.screen, (255, 255, 255), True, self.points_OFFSET) # body
        
    def fire(self):
        print "Pew!" # heh
        # needs to be redefined per weapon, to fire the right bullet in the right manner. Also player weapons shouldn't be firing enemy bullets!
        self.entities.projectilesLayer2.append(Projectiles.Projectile(self.screen, self.barrel, 0, True)) # this is a player weapon!!!
        
        
class TestGun(Weapon):
    def __init__(self, entities, screen, relativeX, relativeY, parent, previousWeaponPoints):
        Weapon.__init__(self, entities, screen, relativeX, relativeY, parent, previousWeaponPoints)